﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;


namespace 医院药库管理系统
{
    public partial class 管理员信息 : Form
    {
        public 管理员信息()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            首页 f3 = new 首页();
            f3.Show();
        }

        private void 管理员信息_Load(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText = "SELECT * FROM tb_Administrator;";
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand;
            DataTable csTable = new DataTable();
            sqlConnection.Open();
            sqlDataAdapter.Fill(csTable);
            sqlConnection.Close();
            this.dgv_gly.DataSource = csTable;
            this.dgv_gly.Columns["No"].HeaderText = "编号";
            this.dgv_gly.Columns["Name"].HeaderText = "名称";
            this.dgv_gly.Columns["Password"].HeaderText = "密码";
        }

        private void bt_xg_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlCommand updateCommand = new SqlCommand();
            updateCommand.Connection = sqlConnection;
            updateCommand.CommandText =
                "UPDATE tb_Administrator"
                + " SET No=@NewNo,Name=@Name,Password=@Password"
                + " WHERE No=@OldNo;";
            updateCommand.Parameters.Add("@NewNo", SqlDbType.Int, 0, "No");
            updateCommand.Parameters.Add("@Name", SqlDbType.VarChar, 100, "Name");
            updateCommand.Parameters.Add("@Password", SqlDbType.VarChar, 20, "Password");
            updateCommand.Parameters.Add("@OldNo", SqlDbType.Int, 0, "No");
            updateCommand.Parameters["@OldNo"].SourceVersion = DataRowVersion.Original;
            SqlCommand deleteCommand = new SqlCommand();                                                    //声明并实例化SQL命令；该命令用于删除；
            deleteCommand.Connection = sqlConnection;                                                       //将SQL命令的连接属性指向SQL连接；
            deleteCommand.CommandText =                                                                     //指定SQL命令的命令文本；
                "DELETE tb_Administrator"
                + " WHERE No=@No;";
            deleteCommand.Parameters.Add("@No", SqlDbType.Int, 0, "No");
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.UpdateCommand = updateCommand;
            sqlDataAdapter.DeleteCommand = deleteCommand;
            DataTable csTable1 = (DataTable)this.dgv_gly.DataSource;
            sqlConnection.Open();
            int rowAffected = sqlDataAdapter.Update(csTable1);
            sqlConnection.Close();
            MessageBox.Show("更新" + rowAffected.ToString() + "行。");
        }
    }
}
